﻿using System;
using UnityEngine;
using Utility;

// Token: 0x020003F1 RID: 1009
public class LookAtRotator_HierarchyRotate : MonoBehaviour
{
	// Token: 0x1700026D RID: 621
	// (get) Token: 0x0600193C RID: 6460 RVA: 0x000A32D0 File Offset: 0x000A14D0
	public LookAtRotator_HierarchyRotate.TYPE CalcType
	{
		get
		{
			return this.calcType;
		}
	}

	// Token: 0x0600193D RID: 6461 RVA: 0x000A32D8 File Offset: 0x000A14D8
	public void Init()
	{
		this.baseLocalRot = this.baseBone.transform.localRotation;
		if (this.rotateBones != null && this.rotateBones.Length > 0)
		{
			for (int i = 0; i < this.rotateBones.Length; i++)
			{
				this.rotateBones[i].Init();
			}
		}
	}

	// Token: 0x0600193E RID: 6462 RVA: 0x000A333C File Offset: 0x000A153C
	public void Calc()
	{
		if (this.calcType == LookAtRotator_HierarchyRotate.TYPE.NO)
		{
			return;
		}
		Vector3 localEulerAngles = this.baseBone.transform.localEulerAngles;
		localEulerAngles.x = Mathf.DeltaAngle(0f, localEulerAngles.x);
		localEulerAngles.y = Mathf.DeltaAngle(0f, localEulerAngles.y);
		localEulerAngles.z = Mathf.DeltaAngle(0f, localEulerAngles.z);
		this.baseBone.transform.localRotation = this.baseLocalRot;
		if (this.target && (this.calcType == LookAtRotator_HierarchyRotate.TYPE.TARGET || this.calcType == LookAtRotator_HierarchyRotate.TYPE.AWAY))
		{
			this.targetPos = this.target.transform.position;
		}
		Vector3 vector = this.baseBone.InverseTransformPoint(this.targetPos);
		Vector3 vector2 = Vector3.zero;
		foreach (LookAtRotator_HierarchyRotate.RotateBone rotateBone in this.rotateBones)
		{
			if (this.calcType == LookAtRotator_HierarchyRotate.TYPE.NO)
			{
				vector2 = localEulerAngles;
			}
			else if (this.calcType == LookAtRotator_HierarchyRotate.TYPE.TARGET)
			{
				VectorUtility.Vector3_ToYawPitch(vector, out vector2.y, out vector2.x);
				vector2.y = Mathf.DeltaAngle(0f, vector2.y);
				vector2.x = Mathf.DeltaAngle(0f, vector2.x) + this.offsetPitch;
				if (this.prevTarget != null && Mathf.Abs(vector2.y) >= this.ignoreYaw)
				{
					VectorUtility.Vector3_ToYawPitch(this.prevTarget.Value, out vector2.y, out vector2.x);
					vector2.y = Mathf.DeltaAngle(0f, vector2.y);
					vector2.x = Mathf.DeltaAngle(0f, vector2.x) + this.offsetPitch;
				}
				else
				{
					this.prevTarget = new Vector3?(vector);
				}
				vector2 *= this.turnRate;
				vector2.y = Mathf.Clamp(vector2.y, -rotateBone.maxLeft, rotateBone.maxRight);
				vector2.x = Mathf.Clamp(vector2.x, -this.maxUp, this.maxDown);
				vector2.z = 0f;
			}
			else if (this.calcType == LookAtRotator_HierarchyRotate.TYPE.FORWARD)
			{
				vector2.y = 0f;
				vector2.x = this.offsetPitch;
				vector2.y = Mathf.Clamp(vector2.y, -rotateBone.maxLeft, rotateBone.maxRight);
				vector2.x = Mathf.Clamp(vector2.x, -this.maxUp, this.maxDown);
				vector2.z = 0f;
			}
			else if (this.calcType == LookAtRotator_HierarchyRotate.TYPE.DIRECTION)
			{
				vector2.y = this.targetPos.y;
				vector2.x = this.targetPos.x + this.offsetPitch;
				vector2.y = Mathf.Clamp(vector2.y, -rotateBone.maxLeft, rotateBone.maxRight);
				vector2.x = Mathf.Clamp(vector2.x, -this.maxUp, this.maxDown);
				vector2.z = 0f;
			}
			else if (this.calcType == LookAtRotator_HierarchyRotate.TYPE.DIRECTION_UNLIMITED)
			{
				vector2.y = this.targetPos.y;
				vector2.x = this.targetPos.x + this.offsetPitch;
				vector2.z = 0f;
			}
			else if (this.calcType == LookAtRotator_HierarchyRotate.TYPE.AWAY)
			{
				Vector3 zero = Vector3.zero;
				VectorUtility.Vector3_ToYawPitch(vector, out zero.y, out zero.x);
				zero.y = Mathf.DeltaAngle(0f, zero.y);
				zero.x = Mathf.DeltaAngle(0f, zero.x) + this.offsetPitch;
				if (this.prevTarget != null && Mathf.Abs(zero.y) <= this.awayIgnoreYaw)
				{
					VectorUtility.Vector3_ToYawPitch(this.prevTarget.Value, out zero.y, out zero.x);
					zero.y = Mathf.DeltaAngle(0f, zero.y);
					zero.x = Mathf.DeltaAngle(0f, zero.x) + this.offsetPitch;
				}
				else
				{
					this.prevTarget = new Vector3?(vector);
				}
				vector2.y = ((zero.y <= 0f) ? rotateBone.maxRight : (-rotateBone.maxLeft));
				vector2.x = ((zero.x <= 0f) ? this.maxDown : (-this.maxUp));
				vector2.z = 0f;
			}
			rotateBone.Interpolation(vector2, this.turnPower, this.turnMaxSpeed, this.turnMinSpeed);
			rotateBone.PrevEuler = rotateBone.Euler;
			Quaternion rhs = Quaternion.Euler(rotateBone.Euler);
			Quaternion rot = this.baseLocalRot * rhs;
			rotateBone.Rotate(rot);
		}
	}

	// Token: 0x0600193F RID: 6463 RVA: 0x000A3870 File Offset: 0x000A1A70
	public void Change(LookAtRotator_HierarchyRotate.TYPE type, Transform tgt, bool force)
	{
		this.calcType = type;
		this.target = tgt;
		this.targetPos = tgt.position;
		this.noRate = ((!force) ? 0f : 1f);
		foreach (LookAtRotator_HierarchyRotate.RotateBone rotateBone in this.rotateBones)
		{
			rotateBone.Change();
		}
	}

	// Token: 0x06001940 RID: 6464 RVA: 0x000A38D8 File Offset: 0x000A1AD8
	public void Change(LookAtRotator_HierarchyRotate.TYPE type, Vector3 tgt, bool force)
	{
		this.calcType = type;
		this.targetPos = tgt;
		this.target = null;
		this.noRate = ((!force) ? 0f : 1f);
		foreach (LookAtRotator_HierarchyRotate.RotateBone rotateBone in this.rotateBones)
		{
			rotateBone.Change();
		}
	}

	// Token: 0x06001941 RID: 6465 RVA: 0x000A393C File Offset: 0x000A1B3C
	private void Awake()
	{
		this.Init();
	}

	// Token: 0x06001942 RID: 6466 RVA: 0x000A3944 File Offset: 0x000A1B44
	private void LateUpdate()
	{
		this.Calc();
	}

	// Token: 0x04001C74 RID: 7284
	[SerializeField]
	private Transform baseBone;

	// Token: 0x04001C75 RID: 7285
	[SerializeField]
	private Transform target;

	// Token: 0x04001C76 RID: 7286
	[SerializeField]
	private Vector3 targetPos;

	// Token: 0x04001C77 RID: 7287
	[SerializeField]
	private LookAtRotator_HierarchyRotate.RotateBone[] rotateBones;

	// Token: 0x04001C78 RID: 7288
	private Vector3? prevTarget;

	// Token: 0x04001C79 RID: 7289
	[Range(0f, 90f)]
	[SerializeField]
	private float maxUp = 45f;

	// Token: 0x04001C7A RID: 7290
	[SerializeField]
	[Range(0f, 90f)]
	private float maxDown = 45f;

	// Token: 0x04001C7B RID: 7291
	[SerializeField]
	[Range(0f, 2f)]
	private float turnRate = 1f;

	// Token: 0x04001C7C RID: 7292
	[Range(-45f, 45f)]
	[SerializeField]
	private float offsetPitch;

	// Token: 0x04001C7D RID: 7293
	[SerializeField]
	private float turnPower = 3f;

	// Token: 0x04001C7E RID: 7294
	[SerializeField]
	private float turnMinSpeed = 1f;

	// Token: 0x04001C7F RID: 7295
	[SerializeField]
	private float turnMaxSpeed;

	// Token: 0x04001C80 RID: 7296
	[SerializeField]
	private float ignoreYaw = 120f;

	// Token: 0x04001C81 RID: 7297
	[SerializeField]
	private float awayIgnoreYaw = 45f;

	// Token: 0x04001C82 RID: 7298
	private Quaternion baseLocalRot;

	// Token: 0x04001C83 RID: 7299
	[SerializeField]
	private LookAtRotator_HierarchyRotate.TYPE calcType;

	// Token: 0x04001C84 RID: 7300
	private float noRate;

	// Token: 0x020003F2 RID: 1010
	public enum TYPE
	{
		// Token: 0x04001C86 RID: 7302
		NO,
		// Token: 0x04001C87 RID: 7303
		TARGET,
		// Token: 0x04001C88 RID: 7304
		AWAY,
		// Token: 0x04001C89 RID: 7305
		FORWARD,
		// Token: 0x04001C8A RID: 7306
		DIRECTION,
		// Token: 0x04001C8B RID: 7307
		DIRECTION_UNLIMITED
	}

	// Token: 0x020003F3 RID: 1011
	[Serializable]
	private class RotateBone
	{
		// Token: 0x1700026E RID: 622
		// (get) Token: 0x06001944 RID: 6468 RVA: 0x000A396C File Offset: 0x000A1B6C
		// (set) Token: 0x06001945 RID: 6469 RVA: 0x000A3974 File Offset: 0x000A1B74
		public Vector3 Euler
		{
			get
			{
				return this.euler;
			}
			set
			{
				this.euler = value;
			}
		}

		// Token: 0x1700026F RID: 623
		// (get) Token: 0x06001946 RID: 6470 RVA: 0x000A3980 File Offset: 0x000A1B80
		// (set) Token: 0x06001947 RID: 6471 RVA: 0x000A3988 File Offset: 0x000A1B88
		public Vector3 PrevEuler
		{
			get
			{
				return this.prevEuler;
			}
			set
			{
				this.prevEuler = value;
			}
		}

		// Token: 0x06001948 RID: 6472 RVA: 0x000A3994 File Offset: 0x000A1B94
		public void Init()
		{
			this.baseRot = this.bone.localRotation;
			this.euler = Vector3.zero;
		}

		// Token: 0x06001949 RID: 6473 RVA: 0x000A39B4 File Offset: 0x000A1BB4
		public void Change()
		{
			this.prevEuler = this.euler;
		}

		// Token: 0x0600194A RID: 6474 RVA: 0x000A39C4 File Offset: 0x000A1BC4
		public void Rotate(Quaternion rot)
		{
			this.bone.localRotation = this.baseRot * rot;
		}

		// Token: 0x0600194B RID: 6475 RVA: 0x000A39E0 File Offset: 0x000A1BE0
		public void LerpAngle(Vector3 rel, float noRate)
		{
			this.euler.x = Mathf.LerpAngle(this.prevEuler.x, rel.x, noRate);
			this.euler.y = Mathf.LerpAngle(this.prevEuler.y, rel.y, noRate);
			this.euler.z = Mathf.LerpAngle(this.prevEuler.z, rel.z, noRate);
		}

		// Token: 0x0600194C RID: 6476 RVA: 0x000A3A58 File Offset: 0x000A1C58
		public void Interpolation(Vector3 rel, float speed, float min, float max)
		{
			this.euler.y = Tween.Spring(this.euler.y, rel.y, speed, Time.deltaTime, min, max);
			this.euler.x = Tween.Spring(this.euler.x, rel.x, speed, Time.deltaTime, min, max);
			this.euler.z = Tween.Spring(this.euler.z, rel.z, speed, Time.deltaTime, min, max);
		}

		// Token: 0x04001C8C RID: 7308
		public Transform bone;

		// Token: 0x04001C8D RID: 7309
		public float maxLeft = 45f;

		// Token: 0x04001C8E RID: 7310
		public float maxRight = 45f;

		// Token: 0x04001C8F RID: 7311
		private Quaternion baseRot;

		// Token: 0x04001C90 RID: 7312
		private Vector3 euler;

		// Token: 0x04001C91 RID: 7313
		private Vector3 prevEuler;
	}
}
